package com.hliushi.redis.controller;

import com.hliushi.redis.entity.Cart;
import com.hliushi.redis.entity.CartPage;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author llhuang10
 * @date 2021/8/7 19:49
 */
@Api(description = "用户登录状态下操作购物车")
@Slf4j
@RestController
@RequestMapping("/cart")
public class UserCartController {


    public static final String CART_KEY = "cart:user:";

    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    /**
     * 添加购物车
     *
     * @param
     * @return
     */
    @PostMapping("/add-cart")
    public void addCart(Cart cart) {
        String key = CART_KEY + cart.getUserId();
        Boolean hasKey = redisTemplate.opsForHash().getOperations().hasKey(key);

        if (hasKey) {
            redisTemplate.opsForHash().put(key, cart.getProductId().toString(), cart.getAmount());
        } else {
            redisTemplate.opsForHash().put(key, cart.getProductId().toString(), cart.getAmount());
            redisTemplate.expire(key, 90, TimeUnit.DAYS);
        }
        // TODO 发rabbitMq 出去
    }


    /**
     * 修改购物车数量
     *
     * @param
     * @return
     */
    @PutMapping("/update-cart")
    public void updateCart(Cart cart) {
        String key = CART_KEY + cart.getUserId();
        redisTemplate.opsForHash().put(key, cart.getProductId().toString(), cart.getAmount());
        // TODO 发rabbitMq 出去
    }

    /**
     * 删除购物车
     *
     * @param
     * @return
     */
    @DeleteMapping("/del-cart")
    public void delCart(Long userId, Long productId) {
        String key = CART_KEY + userId;
        redisTemplate.opsForHash().delete(key, productId.toString());
        // TODO 发rabbitMq 出去
    }

    /**
     * @param
     * @return
     */
    @GetMapping("/find-all")
    public ResponseEntity<CartPage<Cart>> findAll(Long userId) {
        String key = CART_KEY + userId;
        CartPage<Cart> cartPage = new CartPage<>();
        // 查购物车的总数
        long size = redisTemplate.opsForHash().size(key);
        cartPage.setCount((int) size);

        // 查询购物车的所有商品
        // entries = hgetall命令
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        List<Cart> cartList = new ArrayList<>();

        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            Cart cart = new Cart();
            cart.setUserId(userId);
            if (entry.getKey() instanceof String) {
                cart.setProductId(Long.parseLong((String) entry.getKey()));
            }
            if (entry.getValue() instanceof Integer) {
                cart.setAmount((Integer) entry.getValue());
            }
            cartList.add(cart);
        }
        cartPage.setCartList(cartList);

        return ResponseEntity.ok(cartPage);
    }
}
